package org.zjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.zjx.dao.entity.DeviceDailyTask;
import org.zjx.dao.entity.Font;
import org.zjx.dao.mapper.FontMapper;
import org.zjx.service.IFontService;
import org.zjx.common.bean.Result;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2025-05-30
 */
@Service
public class FontServiceImpl extends ServiceImpl<FontMapper, Font> implements IFontService {

    @Override
    public Result saveFont(Font font) {
        LambdaQueryWrapper<Font> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Font::getFontTypeId, font.getFontTypeId())
                .eq(Font::getBlock, font.getBlock());
        Font existingFont = baseMapper.selectOne(queryWrapper);

        if (existingFont != null) {
            return Result.isFail().msg(new Exception("已存在相同字体类型和字块的记录"));
        }

        try {
            // 尝试保存，依赖数据库唯一约束防止并发
            save(font);
            return Result.isOk();
        } catch (DuplicateKeyException e) {
            // 捕获数据库唯一约束异常
            return Result.isFail().msg(new Exception("数据库唯一约束冲突，请检查数据"));
        }
    }

    @Override
    public List<Font> selectByFontTypeId(Long fontTypeId) {
        return lambdaQuery().eq(Font::getFontTypeId, fontTypeId).list();
    }
}
